package com.xx.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xx.springboot.Vo.BlogVo;
import com.xx.springboot.annotation.Log;
import com.xx.springboot.entity.Blog;
import com.xx.springboot.entity.CommonResult;
import com.xx.springboot.entity.ResponseBo;
import com.xx.springboot.entity.User;
import com.xx.springboot.mapper.BlogMapper;
import com.xx.springboot.service.IBlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxiunique
 * @since 2018-08-06
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public ResponseBo findBlogListByBlogVo(BlogVo blogVo) {
        //设置分页参数
        blogVo.setStatus(1);
        blogVo.setIsDelete(0);
        //处理参数  查询没有删除掉的代码
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        blogVo.setUserId(user.getId());
        Map<String, Object> res = new HashMap<>();
        res.put("count", blogMapper.findBlogListByBlogVoCount(blogVo));
        res.put("list", blogMapper.findBlogListByBlogVo(blogVo));
        return ResponseBo.ok(res);
    }

    /**
     * @param categoryId
     * @Author：Xiaoxi
     * @date：2018/2/18 13:16
     * @Version 1.0
     * @UpdateUser: 更新者
     * @UpdateDate 2018/2/18 13:16
     * @Method getBlogListByCate
     * @Params categoryId
     * @Return List<BlogVo>
     * @Description：根据类型查询得到 博客
     */
    public List<BlogVo> getBlogListByCate(int categoryId,int pageNo,int pageSize) {
        BlogVo blogVo = new BlogVo();
        blogVo.setCategoryId(categoryId);
        blogVo.setPageNo(pageNo);
        blogVo.setPageSize(pageSize);
        blogVo.setStatus(1);
        blogVo.setIsDelete(0);
        return blogMapper.findBlogListByBlogVo(blogVo);
    }

    /**
     * 分页查询 , 查询所有的数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public ResponseBo getBlogList(Integer pageNo, Integer pageSize, String searchData) {
       BlogVo blogVo = new BlogVo();
       blogVo.setPageNo(pageNo);
       blogVo.setPageSize(pageSize);
       blogVo.setStatus(1);
       blogVo.setIsDelete(0);
       blogVo.setSearchData(searchData);
        try {
            int count = blogMapper.findBlogListByBlogVoCount(blogVo);
            List<BlogVo> list = blogMapper.findBlogListByBlogVo(blogVo);
            Map<String, Object> res = new HashMap<>();
            res.put("count", count);
            res.put("list", list);
            return ResponseBo.ok(res);
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }

    }

    /**
     * 根据Id 查询文章详情
     *
     * @param blogId
     * @return
     */
    @Override
    public BlogVo getBlogById(Integer blogId) {

        return blogMapper.getBlogById(blogId);
    }

    /**
     * 逻辑删除
     *
     * @param blogId
     * @return
     */
    public int removeBlog(Long blogId) {
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        Blog blog = new Blog();
        blog.setIsDelete(1);

        QueryWrapper<Blog> q = new QueryWrapper<>();
        q.eq("user_id", user.getId());
        q.eq("id", blogId);
        q.eq("is_delete", "0");
        return blogMapper.update(blog, q);
    }
}
